package homework;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 刘俊俊
 * @date 2021/11/28
 * @description
 */
public class Demo02 {
    private static int num = 0;

    public static void main(String[] args) {

        Demo02Source source = new Demo02Source();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    num++;
                    if (num % 5 == 0) {
                        TimeUnit.SECONDS.sleep(1);
                    }
                    source.print();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, "a").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    source.print();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }, "b").start();
    }
}

class Demo02Source {

    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();

    public void print() {


        System.out.println(Thread.currentThread().getName() + "正在运行");


    }
}
